<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\AdminController;

use App\Models\AdminUsers;
use App\Models\AdminRoles;
use App\Models\AdminPermissionRole;

use App\Models\AdminUserPermissions;
use Illuminate\Http\Request;
use Validator , Config , Session , Cookie;


class AdminUsersController extends AdminController {
    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = '';
        $this->_response['_active']['_action']      = '';
    }

    public function doLogin(Request $request){
        $account    = $request->get('account');
        $password   = $request->get('password');

        $user = AdminUsers::where('account' , $account)->first();
        if($user){
            $passwordSalt = encrypt_sha_md5($password , $user->salt);

            if($passwordSalt == $user->password){

                $sessionKey = md5($user->salt . time());

                $user = $user->toArray();
                unset($user['password']);
                unset($user['salt']);

                Session::put($sessionKey);

                $cookie = Cookie::make(Config::get('session.admin_cookie'), $sessionKey, Config::get('session.admin_cookie_lifetime'));

                return response()->json(['code' => 0 , 'msg' => '登录成功'])->withCookie($cookie);
            }else{
                return response()->json(['code' => -1 , 'msg' => '登录失败']);
            }
        }else{
            return response()->json(['code' => -2 , 'msg' => '登录失败']);
        }
    }


    public function getList(){
        $this->_response['_active']['_action']      = 'list';

        $adminUserModel = new AdminUsers();
        $adminPermissionModel       = new AdminUserPermissions();
        $permissionList             = $adminPermissionModel->getPermissionsList();

        $this->_response['permissions'] = $permissionList;
        $this->_response['user'] = $adminUserModel->getUserList();
        $data=$adminUserModel->AdminUsers();

        return response()->json(['code' => 0, 'msg' => '', 'data' => $data]);
    }

    public function getInfo($id){
        $adminUserModel = new AdminUsers();

        $this->_response['user']    = $adminUserModel->getUserInfo($id);
        $this->_response['role']    = AdminRoles::whereNull('deleted_at')->get();
        $data=$adminUserModel->AdminUsers();

        return response()->json(['code' => 0, 'msg' => '', 'data' => $data]);
    }


    public function update(Request $request){

        if($request->has('id')){
            $id = $request->get('id');
        }else{
            return response()->json(['code' => -1 , 'msg' => '参数错误']);
        }

        $saveData = [];
        if($request->has('name')) {
            $saveData['name']       = $request->get('name');
            $saveData['account']    = $saveData['name'];
        }
        if($request->has('password')) {
            $saveData['password']   = $request->get('password');
            $saveData['salt']       = getSalt(8);
            $saveData['password']   = encrypt_sha_md5($saveData['password'] , $saveData['salt']);
        }

        $role = [];

        if($request->has('role') || !empty($role)){
            $role       = $request->get('role');
        }

        $saveData['updated_at'] = date('Y-m-d H:i:s' , time());

        $adminUserModel = new AdminUsers();

        if($adminUserModel->updateUser($id , $saveData , $role)){
            return response()->json(['code' => 0 , 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -2 , 'msg' => '修改失败,请稍后重试']);
        }
    }

    public function updatePermission(Request $request){

        if($request->has('id')){
            $id = $request->get('id');
        }else{
            return response()->json(['code' => -1 , 'msg' => '参数错误']);
        }

        $permission = [];

        if($request->has('permission') ) {
            $permission = $request->get('permission');
        }

        $adminUserModel = new AdminUsers();

        if($adminUserModel->updateUserPermission($id , $permission )){
            return response()->json(['code' => 0 , 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -2 , 'msg' => '修改失败,请稍后重试']);
        }


    }


    public function getPermission($id){

        $userPermission = AdminUserPermissions::where('uid' , $id)->get();

        $permission = [];
        foreach ($userPermission as $up){
            $permission[] = $up->pid;
        }

        return response()->json(['code' => 0 , 'data' => $permission]);
    }

    public function currentLoginUser(){
        global $uuuid;

        $user = AdminUsers::select('name' , 'account' , 'id')->whereNull('deleted_at')->where('id' , $uuuid)->first()->toArray();

        return response()->json(['code' => 0 , 'data' => $user]);
    }

}